package at.evonfc.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import at.evonfc.db.DataBaseAccess;

public class Employee implements Serializable {
	private String employeeID;
	private String employeeName;
	
	private List<Rental> borrowedDevices;
	
	public Employee(String id) throws Exception {
		employeeID = id;
		
		employeeName = DataBaseAccess.getUserName(employeeID);
		if(employeeName == null || employeeName.isEmpty()) {
			throw  new Exception("Kein Mitarbeiter gefunden!");
		}
		updateBorrowedDevices();
	}
	
	public void updateBorrowedDevices() {
		try {
			borrowedDevices = DataBaseAccess.getDevicesFromUser(employeeID);
		} catch (Exception e) {
		}
	}

	public String getEmployeeID() {
		return employeeID;
	}

	public void setEmployeeID(String employeeID) {
		this.employeeID = employeeID;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public List<Rental> getBorrowedDevices() {
		return borrowedDevices;
	}

	public void setBorrowedDevices(List<Rental> borrowedDevices) {
		this.borrowedDevices = borrowedDevices;
	}

	public void removeBorrowedDevice(int deviceID) {
		Rental d = null;
		
		for(Rental bd : borrowedDevices) {
			if(bd.getDevice().getId() == deviceID) {
				d = bd;
				break;
			}
		}
		
		if(d != null) {
			borrowedDevices.remove(d);
		}
		
	}
}
